package com.v2future.sjt.collegecheckingattendance.fruit.research.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.v2future.sjt.collegecheckingattendance.MainFragment;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ResearchTypeAddBean;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.WidgetUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 专利输入项
 *
 * @author shijianting
 * @time 2016/5/20 14:43
 */
public class PatentInputFragment extends MainFragment {

    ResearchTypeAddBean researchTypeAddBean;

    ConditionBean conditionBean;
    @Bind(R.id.tv_code)
    EditText tvCode;
    @Bind(R.id.tv_name)
    EditText tvName;
    @Bind(R.id.tv_level)
    Button tvLevel;
    @Bind(R.id.tv_rank)
    Button tvRank;
    @Bind(R.id.tv_des)
    EditText tvDes;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getArguments()) {
            conditionBean = (ConditionBean) getArguments().getSerializable(IntentKey.CONDITION);
        } else {
            conditionBean = null;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_input_patent, container, false);
        ButterKnife.bind(this, view);
        initView(view);
        return view;
    }

    public void initView(View v) {
        if (null != conditionBean) {
            WidgetUtil.setText(tvCode, conditionBean.getAttr1());
            WidgetUtil.setText(tvName, conditionBean.getAttr2());
            WidgetUtil.setText(tvLevel, conditionBean.getAttr3());
            WidgetUtil.setText(tvRank, conditionBean.getAttr4());
            WidgetUtil.setText(tvDes, conditionBean.getAttr5());
            tvName.setEnabled(false);
            tvCode.setEnabled(false);
            tvLevel.setEnabled(false);
            tvRank.setEnabled(false);
            tvDes.setEnabled(false);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public ResearchTypeAddBean getAddBean() {
        if (TextUtils.isEmpty(tvCode.getText().toString()) ||
                TextUtils.isEmpty(tvName.getText().toString()) ||
                TextUtils.isEmpty(tvLevel.getText().toString()) ||
                TextUtils.isEmpty(tvRank.getText().toString()) ||
                TextUtils.isEmpty(tvDes.getText().toString())) {
            return null;
        }
        ResearchTypeAddBean bean = new ResearchTypeAddBean();
        bean.setAttr1(tvCode.getText().toString());
        bean.setAttr2(tvName.getText().toString());
        bean.setAttr3(tvLevel.getText().toString());
        bean.setAttr4(tvRank.getText().toString());
        bean.setAttr5(tvDes.getText().toString());
        return bean;
    }

    @OnClick({R.id.tv_level, R.id.tv_rank})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_level:
                String strs[] = {"发明专利", "实用新型"};
                getBaseActivity().showListDialog(strs, tvLevel);
                break;
            case R.id.tv_rank:
                String strs2[] = {"第一发明人", "第二发明人"};
                getBaseActivity().showListDialog(strs2, tvRank);
                break;
        }
    }
}
